import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/// 警告框工具类：无动画的平台差异化实现（修复报错）
class AlertDialogUtils {
  /// 显示无动画的平台差异化警告框
  static void showPlatformDialog({
    required BuildContext context,
    required String title,
    required String content,
    String confirmText = "确认",
    String cancelText = "取消",
    required VoidCallback onConfirm,
    VoidCallback? onCancel,
    bool barrierDismissible = false,
  }) {
    if (Theme.of(context).platform == TargetPlatform.iOS) {
      _showCupertinoDialog(
        context: context,
        title: title,
        content: content,
        confirmText: confirmText,
        cancelText: cancelText,
        onConfirm: onConfirm,
        onCancel: onCancel,
        barrierDismissible: barrierDismissible,
      );
    } else {
      _showMaterialDialog(
        context: context,
        title: title,
        content: content,
        confirmText: confirmText,
        cancelText: cancelText,
        onConfirm: onConfirm,
        onCancel: onCancel,
        barrierDismissible: barrierDismissible,
      );
    }
  }

  /// iOS 专用实现（无动画）
  static void _showCupertinoDialog({
    required BuildContext context,
    required String title,
    required String content,
    String confirmText = "确认",
    String cancelText = "取消",
    required VoidCallback onConfirm,
    VoidCallback? onCancel,
    bool barrierDismissible = false,
  }) {
    showDialog(
      context: context,
      barrierDismissible: barrierDismissible,
      builder: (context) => CupertinoAlertDialog(
        title: Text(
          title,
          style: const TextStyle(
            fontSize: 17,
            fontWeight: FontWeight.bold,
            color: Colors.black,
          ),
          textAlign: TextAlign.center,
        ),
        content: Text(
          content,
          style: const TextStyle(fontSize: 14, color: Colors.black87),
          textAlign: TextAlign.center,
        ),
        actions: [
          CupertinoDialogAction(
            onPressed: () {
              Navigator.pop(context);
              onCancel?.call();
            },
            child: Text(
              cancelText,
              style: const TextStyle(color: Colors.blue, fontSize: 16),
            ),
          ),
          CupertinoDialogAction(
            onPressed: () {
              Navigator.pop(context);
              onConfirm();
            },
            child: Text(
              confirmText,
              style: const TextStyle(
                color: Colors.blue,
                fontSize: 16,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// Android 专用实现（无动画，修复报错）
  static void _showMaterialDialog({
    required BuildContext context,
    required String title,
    required String content,
    String confirmText = "确认",
    String cancelText = "取消",
    required VoidCallback onConfirm,
    VoidCallback? onCancel,
    bool barrierDismissible = false,
  }) {
    showDialog(
      context: context,
      barrierDismissible: barrierDismissible,
      // 使用builder而非pageBuilder，通过空动画控制器实现无动画
      builder: (context) {
        // 直接返回对话框组件，不添加任何动画
        return AlertDialog(
          title: Text(title),
          content: Text(content),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.pop(context);
                onCancel?.call();
              },
              child: Text(cancelText),
            ),
            TextButton(
              onPressed: () {
                Navigator.pop(context);
                onConfirm();
              },
              child: Text(confirmText),
            ),
          ],
        );
      },

      // 通过过渡动画设置实现无动画效果
    );
  }
}

// // 示例使用页面
// class ExampleUsagePage extends StatelessWidget {
//   const ExampleUsagePage({super.key});

//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       appBar: AppBar(title: const Text('无动画弹窗示例')),
//       body: Center(
//         child: ElevatedButton(
//           onPressed: () {
//             AlertDialogUtils.showPlatformDialog(
//               context: context,
//               title: "提示",
//               content: "这是一个无动画的平台风格弹窗",
//               confirmText: "确认",
//               cancelText: "取消",
//               onConfirm: () => ScaffoldMessenger.of(
//                 context,
//               ).showSnackBar(const SnackBar(content: Text('已确认'))),
//               onCancel: () => ScaffoldMessenger.of(
//                 context,
//               ).showSnackBar(const SnackBar(content: Text('已取消'))),
//             );
//           },
//           child: const Text('显示无动画弹窗'),
//         ),
//       ),
//     );
//   }
// }
